<?php
namespace app\restaurant\controller;
use app\AdminRest;

use app\ApiRest;
use app\diy\model\DiyModel;
use app\member\info\PermissionMember;
use app\restaurant\model\Cate;
use app\restaurant\model\Feeding;
use app\restaurant\model\Store;
use app\restaurant\model\StoreStaff;
use think\App;

use app\restaurant\model\Goods as model;

use app\restaurant\model\Coupon;

use app\restaurant\model\CouponRecord;

use think\facade\Db;


class IndexGoods extends ApiRest
{

    protected $model;

    protected $coupon_model;

    protected $coupon_record_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->coupon_model = new Coupon();

        $this->coupon_record_model = new CouponRecord();
        //处理过期优惠券
        $this->coupon_record_model->endRecord($this->getUserId());

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-16 09:22
     * @功能说明:分类列表
     *
     */
    public function cateList(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];

        $cate_model = new Cate();

        $data = $cate_model->where($dis)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-16 10:26
     * @功能说明:商品列表
     */
    public function goodsList(){

        $input = $this->_param;

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['cate_id','=',$input['cate_id']];

        $dis[] = ['store_id','=',$this->getUserInfo()['store_id']];
        //外卖
        if($input['type']==0){

            $dis[]   = ['out_foods','=',1];
         //堂食
        }elseif($input['type']==1){

            $dis[] = ['house_foods','=',1];
        //自提
        }else{

            $dis[] = ['self_foods','=',1];

        }

        $data  = $this->model->indexGoodsList($dis);

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2020-10-20 14:56
     * @功能说明:餐饮首页
     */
    public function index(){

        //获取diy的数据
        $diy_data = DiyModel::where([['status', '=', 1],['uniacid', '=', $this->_uniacid]])->find();
        //获取商城的
        $page_data= json_decode($diy_data['page'], true)[40] ?? [];
        //判断有没有diy过
        if(empty($page_data)|| (array_key_exists('list',$page_data) && empty($page_data['list']))){

            $page_data = longbing_default_Page(40);

        }
        foreach ($page_data['list'] as &$value){

            if($value['type']=='couponList'&&empty($value['data']['dataList'])) {
                //可以领取都卡券
                $coupon = $this->coupon_model->couponCanGetlimit($this->_uniacid,$this->getUserId());

                if(!empty($coupon)){

                    if($value['data']['type']==2){

                        $value['data']['dataList'] = $coupon;

                    }else{

                        $value['data']['dataList']['coupon'] = $coupon[0];


                        $value['data']['dataList']['coupon_last_record'] = $this->coupon_record_model->where(['coupon_id'=> $coupon[0]['id']])->limit(3)->order('id desc')->select()->toArray();

                        if ( !empty(  $value['data']['dataList']['coupon_last_record'] ) )
                        {

                            foreach (  $value['data']['dataList']['coupon_last_record'] as $index => $item )
                            {
//
                                $user = longbingGetUser($item[ 'user_id' ] ,$this->_uniacid);

                                if ( mb_strlen( $user[ 'nickName' ], 'utf8' ) > 4 )
                                {
                                    $user[ 'nickName' ] = mb_substr( $user[ 'nickName' ], 0, 4, "UTF-8" );
                                }

                                $value['data']['dataList']['coupon_last_record'][ $index ][ 'user_info' ] = $user;
                            }

                        }
                    }
                }
            }
        }

        return $this->success($page_data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-21 17:25
     * @功能说明:商品规格
     */

    public function goodsSpe(){

        $input = $this->_param;

        $data  = [];

        $data  = $this->model->goodsSpeList($input['id'],$data);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-26 15:35
     * @功能说明:查看是否有会员插件
     */

    public function haveMemberAuth(){

        $member_p = new PermissionMember($this->_uniacid);

        $auth = $member_p->pAuth();

        return $this->success($auth);

    }









}
